define(['jquery', 'avalon', 'util', 'text!/loan/apply.html', 'layer'], function(
  $,
  avalon,
  util,
  tmpl,
  layer
) {
  $('#model-view').html(tmpl)

  var apply = avalon.define({
    $id: 'product-apply',
    managerId: '',
    productId: '',
    apply: function(managerId, productId) {
      if (!util.needLogin()) {
        return
      }
      this.managerId = managerId
      this.productId = productId
      // 判断是否完善资料
      if (localStorage.isProfile && localStorage.isProfile == 1) {
        // 提交申请
        this.submit(managerId)
      } else {
        $('.model-apply').show()
      }
    },
    submit: function() {
      util.ajax({
        url: '/member/apply/loan',
        type: 'post',
        data: {
          productId: this.productId,
          managerId: this.managerId
        },
        success: function(rsp) {
          if (rsp.code == 200) {
            $('#applydialog').show()
          } else {
            console.log(rsp.code)
            // layer.alert(rsp.msg, {
            //   skin: 'demo-class',
            //   closeBtn: 0
            // })
            alert(rsp.msg)
            $('.model-apply').hide()
          }
        }
      })
    }
  })

  // apply-tab hide
  $('.model-apply-close-btn').on('click', function() {
    $('.model-apply').hide()
  })
  // apply hide
  $('#applyclose-btn').on('click', function() {
    $('#applydialog').hide()
    $('.model-apply').hide()
  })
  // apply hide
  $('#profile-btn').on('click', function() {
    $('#profiledialog').hide()
  })
  return apply
})
